package com.rbac.admin.service.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rbac.admin.entity.system.SystemDept;
import com.rbac.admin.mapper.system.DeptMapper;
import com.rbac.admin.service.system.DeptService;
import com.rbac.admin.vo.system.DeptVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 部门服务实现类
 *
 * @author rbac-admin
 * @since 2024-07-18
 */
@Slf4j
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, SystemDept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<DeptVO> getDeptList(SystemDept dept) {
        return deptMapper.selectDeptList(dept);
    }

    @Override
    public List<DeptVO> getDeptTree(SystemDept dept) {
        List<DeptVO> deptList = deptMapper.selectDeptTree(dept);
        return buildDeptTree(deptList);
    }

    @Override
    public DeptVO getDeptById(Integer deptId) {
        return deptMapper.selectDeptById(deptId);
    }

    @Override
    public boolean checkDeptNameExists(String deptName, Integer excludeDeptId) {
        Integer count = deptMapper.checkDeptNameExists(deptName, excludeDeptId);
        return count != null && count > 0;
    }

    @Override
    public boolean checkDeptCodeExists(String deptCode, Integer excludeDeptId) {
        Integer count = deptMapper.checkDeptCodeExists(deptCode, excludeDeptId);
        return count != null && count > 0;
    }

    @Override
    public boolean hasChildDept(Integer deptId) {
        Integer count = deptMapper.selectChildrenDeptCount(deptId);
        return count != null && count > 0;
    }

    @Override
    public boolean hasUserByDept(Integer deptId) {
        Integer count = deptMapper.hasUserByDeptId(deptId);
        return count != null && count > 0;
    }
    
    @Override
    public List<Map<String, Object>> getUserList() {
        return deptMapper.selectUserList();
    }

    @Override
    public List<DeptVO> buildDeptTree(List<DeptVO> deptList) {
        if (deptList == null || deptList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 将部门列表转换为Map，便于查找
        Map<Integer, DeptVO> deptMap = deptList.stream()
                .collect(Collectors.toMap(DeptVO::getId, dept -> dept, (a, b) -> a));
        
        // 构建树形结构
        List<DeptVO> rootDeptList = new ArrayList<>();
        for (DeptVO dept : deptList) {
            // 找到根节点
            if (dept.getParentId() == null || dept.getParentId() == 0) {
                rootDeptList.add(dept);
            } else {
                // 将当前节点添加到父节点的children列表中
                DeptVO parentDept = deptMap.get(dept.getParentId());
                if (parentDept != null) {
                    if (parentDept.getChildren() == null) {
                        parentDept.setChildren(new ArrayList<>());
                    }
                    parentDept.getChildren().add(dept);
                } else {
                    // 如果父节点不存在，将当前节点作为根节点
                    rootDeptList.add(dept);
                }
            }
        }
        
        return rootDeptList;
    }
} 